package cn.edu.xmu.oomall.comment.controller;


import cn.edu.xmu.javaee.core.aop.LoginUser;
import cn.edu.xmu.javaee.core.exception.BusinessException;
import cn.edu.xmu.javaee.core.model.ReturnNo;
import cn.edu.xmu.javaee.core.model.ReturnObject;
import cn.edu.xmu.javaee.core.model.dto.UserDto;
import cn.edu.xmu.oomall.comment.service.CommentService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static cn.edu.xmu.javaee.core.model.Constants.PLATFORM;

@RestController /*Restful的Controller对象*/
@RequiredArgsConstructor
@RequestMapping(produces = "application/json;charset=UTF-8")
public class AdminController {

    private final CommentService commentService;

    /**
     * 管理员审核评论
     * @param dId
     * @param id
     * @param user
     * @return
     */
    @PutMapping("/admin/{did}/comments/{id}/audit")
    public ReturnObject auditComment(@PathVariable(value = "did") Long dId,
                                     @PathVariable Long id,
                                     @LoginUser UserDto user) {
        if (!PLATFORM.equals(dId)) {
            throw new BusinessException(ReturnNo.RESOURCE_ID_OUTSCOPE, String.format(ReturnNo.RESOURCE_ID_OUTSCOPE.getMessage(), "顾客", id, dId));
        }
        commentService.auditComment(id, user);
        return new ReturnObject();
    }

    /**
     * 管理员审核评论
     * @param dId
     * @param id
     * @param user
     * @return
     */
    @PutMapping("/admin/{did}/comments/{id}/hide")
    public ReturnObject hideComment(@PathVariable(value = "did") Long dId,
                                     @PathVariable Long id,
                                     @LoginUser UserDto user) {
        if (!PLATFORM.equals(dId)) {
            throw new BusinessException(ReturnNo.RESOURCE_ID_OUTSCOPE, String.format(ReturnNo.RESOURCE_ID_OUTSCOPE.getMessage(), "顾客", id, dId));
        }
        commentService.hideComment(id, user);
        return new ReturnObject();
    }
}
